//
//  YMDayliyListVC.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/14.
//  Copyright © 2018年 youmy. All rights reserved.
//  每日上新

import UIKit
import MJRefresh

fileprivate let kCellId = "kCellId"

class YMDayliyListVC: UIViewController {
    
    lazy var noDataView = YMNoMoreDataView.nibView()
    
    
    lazy var tableView:UITableView = {
        let tableView = UITableView(frame: self.view.bounds)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = ym_bg_color
        tableView.register(UINib.init(nibName: "YMDailyListCell", bundle: nil), forCellReuseIdentifier: kCellId)
        tableView.tableFooterView = UIView()
        tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15)
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }

        view.addSubview(tableView)
        
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
                self.tableView.mj_header.endRefreshing()
            })
        })
        
        
        let refreshFooter = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(self.loadMore))
        refreshFooter?.setTitle("点击或上拉刷新", for: .idle)
        refreshFooter?.setTitle("加载更多数据...", for: .refreshing)
        refreshFooter?.setTitle("没有更多了哟~", for: .noMoreData)
        refreshFooter?.stateLabel.font = UIFont.systemFont(ofSize: 11)
        refreshFooter?.stateLabel.textColor = ym_light_color
        refreshFooter?.backgroundColor = ym_bg_color
        tableView.mj_footer = refreshFooter
    }
    
    @objc func loadMore(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
            self.tableView.mj_footer.endRefreshingWithNoMoreData()
        })
    }
}

extension YMDayliyListVC: UITableViewDataSource, UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 123.0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellId, for: indexPath) as! YMDailyListCell
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
